cc.Class({
    extends: cc.Component,

    properties: {
        //飞机移动的速度，不带方向
        moveSpeed: 0,

        //飞机移动的速度，带方向
        _xSpeed: 0,
    },

    // use this for initialization
    onLoad: function () {
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
    },

    update: function(dt){
        var targetX = this.node.x + this._xSpeed * dt;
        if(targetX < -480 + this.node.width/2 || targetX > 480-this.node.width/2){
            return;
        }
        this.node.x = targetX;
    },

    onCollisionEnter: function (other, self) {
        this.game.dropBlood();
    },

    onKeyDown (event) {
        switch(event.keyCode) {
            case cc.KEY.a:
            case cc.KEY.left:
                this.turnLeft();
                break;
            case cc.KEY.d:
            case cc.KEY.right:
                this.turnRight();
                break;
            case cc.KEY.back:
                cc.director.end();
        }
    },

    turnLeft: function(){
        this._xSpeed = -this.moveSpeed;
    },

    turnRight: function(){
        this._xSpeed = this.moveSpeed;
    },
});
